package com.kye.express.business.waybill_list;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;

import com.jakewharton.rxbinding2.view.RxView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.express.R;
import com.kye.express.business.base.BaseAppCompatActivity;
import com.kye.express.databinding.ActivityWaybillListBinding;
import com.kye.model.bean.waybill_list.WaybillStatusType;

import io.reactivex.functions.Consumer;

public class WaybillListActivity extends BaseAppCompatActivity implements WaybillListContract.WaybillListView {

    private final static String TAG_WAYBILL_STATUS_TYPE = "TAG_WAYBILL_STATUS_TYPE";
    protected ActivityWaybillListBinding binding;
    protected WaybillListContract.WaybillListPresenter presenter;
    private String[] titlesSign = {
            "待签收",
            "已签收",
    };
    private WaybillStatusType waybillStatusType;
    protected WaybillListPageAdapter adapter;

    public static Intent newIntent(Context context, WaybillStatusType waybillStatusType) {
        Intent intent = new Intent(context, WaybillListActivity.class);
        intent.putExtra(TAG_WAYBILL_STATUS_TYPE, waybillStatusType);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState == null) {
            waybillStatusType = (WaybillStatusType) getIntent().getSerializableExtra(TAG_WAYBILL_STATUS_TYPE);
        } else {
            waybillStatusType = (WaybillStatusType) savedInstanceState.getSerializable(TAG_WAYBILL_STATUS_TYPE);
        }
        binding = DataBindingUtil.setContentView(this, R.layout.activity_waybill_list);
        presenter = new WaybillListContract.WaybillListPresenter();
        presenter.bind(this);
        presenter.onCreate(savedInstanceState);
        initToolbar(savedInstanceState);
        initViews(savedInstanceState);
        initEvents(savedInstanceState);
        initData(savedInstanceState);
    }

    private void initToolbar(Bundle savedInstanceState) {
        RxView.clicks(binding.back)
                .compose(this.bindToLifecycleDestroy())
                .compose(new DebounceObservableTransformer<Object>())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        finish();
                    }
                });
    }

    private void initViews(Bundle savedInstanceState) {
        adapter = new WaybillListPageAdapter(getSupportFragmentManager());
        binding.waybillPages.setOffscreenPageLimit(2);
        binding.waybillPages.setAdapter(adapter);
        binding.tabLayout.setViewPager(binding.waybillPages, titlesSign);
        switch (waybillStatusType) {
            case WAYBILL_TO_SIGN:
                binding.waybillPages.setCurrentItem(0);
                break;
            case WAYBILL_SIGNED:
                binding.waybillPages.setCurrentItem(1);
                break;
            default:
                throw new IllegalStateException("waybillStatusType error: " + waybillStatusType);
        }
    }

    private void initEvents(Bundle savedInstanceState) {

    }

    private void initData(Bundle savedInstanceState) {

    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putSerializable(TAG_WAYBILL_STATUS_TYPE, waybillStatusType);
        presenter.onSaveInstanceState(outState);
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onDestroy() {
        presenter.unbind();
        super.onDestroy();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onError() {

    }

    @Override
    public void onError(String msg) {

    }

    @Override
    public void onLoading() {

    }

    @Override
    public void onLoaded() {

    }
}
